package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.LossArchitecture;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.LossStructure;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.ReportPublicField;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.EarthquakeDisasterLossRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.strategy.StrategyService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/10
 * @Description: 建（构）筑物地震灾害损失报表
 */
//@Service("建（构）筑物地震灾害损失报表")
@Service("13")
public class EarthquakeDisasterLossServiceImpl implements StrategyService {
    @Resource
    EarthquakeDisasterLossRepository earthquakeDisasterLossRepository;
    @Resource
    ReportPublicFieldRepository reportPublicFieldRepository;

    @Override
    public void reportImport(Workbook workbook, String xlrmId, String previousXlrmId, ReportPublicField reportPublicField) {
        if(!PlatformObjectUtils.isEmpty(previousXlrmId)){
            earthquakeDisasterLossRepository.deleteByXlrmId(previousXlrmId);
            //删除报表公共字段
            reportPublicFieldRepository.deleteByXlrmId(previousXlrmId);
        }
        Map map = getList(workbook,xlrmId);
        List<LossArchitecture> architectureList = (List<LossArchitecture>) map.get("rchitecture");
        List<LossStructure> structureList = (List<LossStructure>) map.get("structure");
        earthquakeDisasterLossRepository.batchArchitectureSave(architectureList);
        earthquakeDisasterLossRepository.batchStructureSave(structureList);
        reportPublicField.setProvince(architectureList.get(0).getProvince());
        reportPublicField.setCity(architectureList.get(0).getCity());
        reportPublicField.setCounty(architectureList.get(0).getCounty());
        //默认删除
        reportPublicField.setDelFlag(YNEnum.Y.toString());
        reportPublicFieldRepository.save(reportPublicField);
    }
    //获取list
    private Map getList(Workbook workbook, String xlrmId){
        HashMap map = new HashMap<>();
        //建筑物部分
        List<LossArchitecture> architectureList = new ArrayList<>();
        //构筑物部分
        List<LossStructure> structureList = new ArrayList<>();
        if (workbook != null) {
            //循环除了第一行的所有行
            /** 得到第二个sheet */
            Sheet sheet = workbook.getSheetAt(0);
            /** 得到Excel的行数 */
            int totalRows = sheet.getPhysicalNumberOfRows();
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            //填表人
            String fillFormUser = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(1));
            //联系电话
            String tel = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(3));
            //填表日期
            String fillFormTime = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(5));
            //省
            String province = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(1));
            //市
            String city = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(3));
            //县
            String district = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(5));
            //村委会
            String village = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(7));
            /** 循环Excel的行 从第六行开始*/
            for (int r = 5; r < totalRows; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                //建筑物部分
                if (r<16){
                    LossArchitecture architecture = new LossArchitecture();
                    //主键id
                    architecture.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    //报表主表id
                    architecture.setXlrmId(xlrmId);
                    //填表人
                    architecture.setFillFormUser(fillFormUser);
                    //tel
                    architecture.setTel(tel);
                    //填报日期
                    architecture.setFillFormTime(fillFormTime);
                    //省
                    architecture.setProvince(province);
                    //市
                    architecture.setCity(city);
                    //县
                    architecture.setCounty(district);
                    //村委会
                    architecture.setVillage(village);
                    //分类
                    architecture.setClassification(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                    //总数量（m2）
                    architecture.setTotalQuantity(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                    //土建
                    architecture.setCivilEngineering(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                    //装修
                    architecture.setDecoration(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                    //基本完好
                    architecture.setGood(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                    //轻微破坏
                    architecture.setMinorDamage(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                    //中等破坏
                    architecture.setModerateDamage(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                    //严重破坏
                    architecture.setSevereDamage(hSSFDataFormatter.formatCellValue(row.getCell(8)));
                    //毁坏
                    architecture.setDestroy(hSSFDataFormatter.formatCellValue(row.getCell(9)));
                    //经济损失
                    architecture.setEconomicLoss(hSSFDataFormatter.formatCellValue(row.getCell(10)));
                    architecture.setSequence(String.valueOf(r));
                    architectureList.add(architecture);
                }
                //构筑物部分
                if (r>16){
                    LossStructure structure = new LossStructure();
                    structure.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    structure.setXlrmId(xlrmId);
                    structure.setFillFormUser(fillFormUser);
                    structure.setTel(tel);
                    structure.setFillFormTime(fillFormTime);
                    structure.setProvince(province);
                    structure.setCity(city);
                    structure.setCounty(district);
                    structure.setVillage(village);
                    //分类
                    structure.setClassification(hSSFDataFormatter.formatCellValue(row.getCell(1)));
                    //总数量（m2）
                    structure.setTotalQuantity(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                    //平均造价
                    structure.setAverageCost(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                    //计量单位
                    structure.setUnitOfMeasurement(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                    //破坏现象描述
                    structure.setFailureDesc(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                    //经济损失(万元)
                    structure.setEconomicLoss(hSSFDataFormatter.formatCellValue(row.getCell(10)));
                    structure.setSequence(String.valueOf(r));
                    structureList.add(structure);
                }
            }
        }
        map.put("rchitecture",architectureList);
        map.put("structure",structureList);
        return map;
    }
}
